package edu.gznu.ics.fast.pulsar.service.userService.impl;


import edu.gznu.ics.fast.pulsar.dao.user.uiResourceMapper;
import edu.gznu.ics.fast.pulsar.model.po.user.uiResourcePO;
import edu.gznu.ics.fast.pulsar.service.userService.ResourceService;
import org.apache.shiro.authz.permission.WildcardPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 14-2-14
 * <p>Version: 1.0
 */
@Service("resourceService")

public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private uiResourceMapper uiResourceMapper;

    @Override
    public uiResourcePO createResource(uiResourcePO resource) {
        return uiResourceMapper.insertResource(resource);
    }

    @Override
    public uiResourcePO setResource(uiResourcePO resource) {
        return uiResourceMapper.updateResource(resource);
    }

    @Override
    public void deleteResource(Long resourceId) {
        uiResourceMapper.deleteResourceByResourceId(resourceId);
    }

    @Override
    public uiResourcePO findResourceByResourceId(Long resourceId) {
        return uiResourceMapper.queryResourceByResourceId(resourceId);
    }

    @Override
    public List<uiResourcePO> findAllResource() {
        return uiResourceMapper.queryAllResource();
    }

    @Override
    public Set<String> findPermissions(Set<Long> resourceIds) {
        Set<String> permissions = new HashSet<String>();
        for (Long resourceId : resourceIds) {
            uiResourcePO resource = findResourceByResourceId(resourceId);
            if (resource != null && !StringUtils.isEmpty(resource.getPermission())) {
                permissions.add(resource.getPermission());
            }
        }
        return permissions;
    }

    @Override
    public List<uiResourcePO> findMenus(Set<String> permissions) {
        List<uiResourcePO> allResources = findAllResource();
        List<uiResourcePO> menus = new ArrayList<uiResourcePO>();
        for (uiResourcePO resource : allResources) {
          /*  if(resource.isRootNode()) {
                continue;
            }
            if(resource.getType() != uiResourcePO.ResourceType.menu) {
                continue;
            }
            if(!hasPermission(permissions, resource)) {
                continue;
            }
            menus.add(resource);*/
        }
        return menus;
    }

    private boolean hasPermission(Set<String> permissions, uiResourcePO resource) {
        if (StringUtils.isEmpty(resource.getPermission())) {
            return true;
        }
        for (String permission : permissions) {
            WildcardPermission p1 = new WildcardPermission(permission);
            WildcardPermission p2 = new WildcardPermission(resource.getPermission());
            if (p1.implies(p2) || p2.implies(p1)) {
                return true;
            }
        }
        return false;
    }
}
